package com.fz.us.oa.web.controller.main.company;

import com.fz.us.admin.base.utils.EntityUtil;
import com.fz.us.admin.company.entity.*;
import com.fz.us.admin.company.service.*;
import com.fz.us.oa.web.controller.BaseController;
import com.fz.us.oa.web.shiro.bind.annotation.CurrentUser;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 控制器 - 岗位信息
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-08-09 。
 * ============================================================================
 */
@Controller
@RequestMapping(value = "/main/commoncfg")
public class CommoncfgController extends BaseController {


    private static final long serialVersionUID = 6000682059514111255L;
    @Autowired
    private CommonConfigService commonConfigService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private PostService postService;
    @Autowired
    private GroupService groupService;
    @Autowired
    private PowerService powerService;



    /**
     * 保存操作
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Object save(@CurrentUser User user, String keyId,@RequestParam(required = false, value = "type") List<String> type,String variable,String allMans,
                      String groupId,String departId,String postId,String powerId,String selectType ) {
        //LogUtil.info("type:"+type.size());
        Company company = user.getCompany();
        CommonConfig commonConfig = null;
        if(StringUtils.isNotEmpty(keyId)){
            commonConfig=commonConfigService.get(keyId);
        }else{
            commonConfig=new CommonConfig();
        }
        Map<String,Object> rMap=new HashMap<String,Object>();

        commonConfig.setStype(getStype(type));
        commonConfig.setVariable(variable);
        if(StringUtils.equals(allMans,"1")){
            commonConfig.setAllmans(true);
        }else{
            commonConfig.setAllmans(false);
        }
        commonConfig.setGroupSet(getGroupSpit(groupId));
        commonConfig.setDepartmentSet(getDepartSpit(departId));
        commonConfig.setPostSet(getPostSpit(postId));
        commonConfig.setPowerSet(getPowerSpit(powerId));
        commonConfig.setCreater(user);
        commonConfig.setCompany(company);
        if(StringUtils.isNotEmpty(keyId)){
            if(type!=null && type.size()>0){
                if(!type.contains("1")){
                    commonConfig.setAllmans(false);
                }
                if(!type.contains("2")){
                    commonConfig.setDepartmentSet(null);
                }
                if(!type.contains("3")){
                    commonConfig.setPostSet(null);
                }
                if(!type.contains("4")){
                    commonConfig.setPowerSet(null);
                }
                if(!type.contains("5")){
                    commonConfig.setGroupSet(null);
                }
                if(!type.contains("6")){
                    commonConfig.setVariable(null);
                }
            }else{
                commonConfig.setGroupSet(null);
                commonConfig.setDepartmentSet(null);
                commonConfig.setPostSet(null);
                commonConfig.setPowerSet(null);
                commonConfig.setVariable(null);
                commonConfig.setAllmans(false);
            }
            commonConfigService.update(commonConfig);
            keyId = commonConfig.getId();
            rMap.put("message", "更新成功");
        }else{
            keyId = commonConfigService.save(commonConfig);
            rMap.put("message","保存成功");
        }
        rMap.put("state","1");
        rMap.put("keyId", keyId);
        rMap.put("selectType",selectType);
        return ajaxJson(rMap);
    }

    /**
     * 将type数组转字符串
     * @param typeList
     * @return
     */
    private String getStype(List<String> typeList){
        String stype="";
        if(typeList != null){
            for(String type : typeList){
                stype += type;
            }
        }
        return stype;
    }

    /**
     * 返回部门集合
     * @param str
     * @return
     */
    private Set<Department> getDepartSpit(String str){
        Set<Department> departSet=new HashSet<Department>();
        String[] ary=str.split(",");
        for(String item:ary){
            if(StringUtils.isNotEmpty(item)) {
                departSet.add(departmentService.get(item));
            }
        }
        return  departSet;
    }

    /**
     * 返回岗位集合
     * @param str
     * @return
     */
    private Set<Post> getPostSpit(String str){
        Set<Post> postSet=new HashSet<Post>();
        String[] ary=str.split(",");
        for(String item:ary){
            if(StringUtils.isNotEmpty(item)) {
                postSet.add(postService.get(item));
            }
        }
        return  postSet;
    }

    /**
     * 返回群组集合
     * @param str
     * @return
     */
    private Set<Group> getGroupSpit(String str){
        Set<Group> groupSet = Sets.newHashSet();
        String[] ary=str.split(",");
        for(String item:ary){
            if(StringUtils.isNotEmpty(item)) {
                groupSet.add(groupService.get(item));
            }
        }
        return  groupSet;
    }
    private Set<Power> getPowerSpit(String str){
        Set<Power> powerSet = Sets.newHashSet();
        String[] ary=str.split(",");
        for(String item:ary){
            if(StringUtils.isNotEmpty(item)) {
                powerSet.add(powerService.get(item));
            }
        }
        return  powerSet;
    }
    /**
     * 编辑，新建
     */
    @RequestMapping(value = "input", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Object input(@CurrentUser User user,Model model, String keyId, String tableKey,String selectType) {
        CommonConfig commonConfig = null;
        String sType = null;
        Set<String> fieldNames = Sets.newHashSet();
        if (StringUtils.isNotEmpty(keyId)){
            commonConfig=commonConfigService.get(keyId);
            sType = commonConfig.getStype();
            if(StringUtils.isNotEmpty(tableKey)){
                Class table = null;
                try {
                    table = Class.forName(tableKey);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                String[] allField =  EntityUtil.getFiledName(table);
                for(String field:allField){
                    if(field.startsWith("duty") == true){
                        fieldNames.add(field);
                    }
                }
            }
        }
        model.addAttribute("commonConfig",commonConfig);
        model.addAttribute("sType",sType);
        model.addAttribute("fieldNames",fieldNames);
        model.addAttribute("selectType",selectType);
        return "/main/company/commoncfg_input";
    }

    /**
     * 获取详情
     */
    @RequestMapping(value = "read", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Object read(@CurrentUser User user,Model model, String keyId) {
        CommonConfig commonConfig = null;
        String sType = null;
        if (StringUtils.isNotEmpty(keyId)){
            commonConfig=commonConfigService.get(keyId);
        }
        model.addAttribute("commonConfig",commonConfig);
        return "/main/company/commoncfg_read";
    }




}
